﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using KSATouch;
using KSASupport;
using Performance;
using KSAWPFDemo;
using KSAWPFExtensions;

namespace KSAWPFDemo.Visualizations
{
    /// <summary>
    /// Interaction logic for Visualizations.xaml
    /// </summary>
    public partial class Visualizations : UserControl
    {
        private DispatcherTimer timer;
        private KSATouch.KSATouch ksaTouch;
        private DepthManager activeDepthManager;
        CalibrationWindow calibrationWindow;
        bool IsKinectRunning = false;

        public Visualizations()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(33);
            timer.Tick += Tick;
            timer.Start();
            this.Loaded += Visualizations_Loaded;
            this.Unloaded += Visualizations_Unloaded;
        }

        void Visualizations_Unloaded(object sender, RoutedEventArgs e)
        {

        }

        void Visualizations_KeyDown(object sender, KeyEventArgs e)
        {
            if (!IsKinectRunning)
                return;

            if (e.Key == Key.Z)
            {
                calibrationWindow.SetMode(DebugWindowMode.Raw);
            }
            if (e.Key == Key.A)
            {
                calibrationWindow.SetMode(DebugWindowMode.HoverThreshold);
            }
            if (e.Key == Key.D)
            {
                calibrationWindow.SetMode(DebugWindowMode.Analysis);
            }
            if (e.Key == Key.S)
            {
                calibrationWindow.SetMode(DebugWindowMode.Threshold);
            }


        }

        void Visualizations_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                KSAWPFWindow mainWindow = KSAWPFWindow.GetWindow(this) as KSAWPFWindow;
                ksaTouch = mainWindow.KsaTouch;
                calibrationWindow = new CalibrationWindow(ksaTouch.DepthManagers[0]);
                calibrationWindow.SetMode(DebugWindowMode.Threshold);
                mainGrid.Children.Add(calibrationWindow);
                calibrationWindow.StartRendering();
                mainWindow.KeyDown += Visualizations_KeyDown;
                IsKinectRunning = true;
            }
            catch (Exception)
            {
                IsKinectRunning = false;
            }
        }
        
        void Tick(object sender, EventArgs e)
        {
            RefreshCurrentDebugWindow();
        }

        private void RefreshCurrentDebugWindow()
        {
            
           // PerformanceMetrics.Content = Performance.PerformanceManager.GetAllPerformanceData();
        }
    }
}
